package com.mashibing.strategy.filter.impl;

import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.StrategyException;
import com.mashibing.common.model.StandardSubmit;
import com.mashibing.strategy.client.BeaconCacheClient;
import com.mashibing.strategy.filter.StrategyFilter;
import com.mashibing.strategy.util.ErrorSendMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author SNA
 * @date 2025/8/25
 * @Description  客户级别黑名单校验
 **/
@Service(value = "blackClient")
@Slf4j
public class BlackClientStrategyFilter implements StrategyFilter {

    @Autowired
    private ErrorSendMsgUtil errorSendMsgUtil;

    @Autowired
    private BeaconCacheClient beaconCacheClient;


    // 黑名单的默认value
    private final String TRUE = "1";


    @Override
    public void strategy(StandardSubmit submit) {
        log.info("【策略模块-客户级别黑名单校验】   校验ing.........");

        //1、获取发送短信的手机号码，以及客户的id
        String mobile = submit.getMobile();
        Long clientId = submit.getClientId();

        //2、调用Redis查询
        String value = beaconCacheClient.getString(CacheConstant.BLACK + clientId + CacheConstant.SEPARATE + mobile);

        //3、如果查询的结果为1，代表是黑名单
        if (TRUE.equals(value)) {
            log.info("【策略模块-客户级别黑名单校验】 当前手机号是客户黑名单！mobile = {}", mobile);
            submit.setErrorMsg(ExceptionEnums.ERROR_CLIENT.getMsg() + "mobile = " + mobile);

            errorSendMsgUtil.sendWriteLog(submit);
            errorSendMsgUtil.sendPushReport(submit);

            throw new StrategyException(ExceptionEnums.ERROR_CLIENT);
        }

        //4、如果不是1，正常结束
        log.info("【策略模块-客户级别黑名单校验】 当前手机号码不是客户黑名单！");
    }
}
